<?php require_once('../connections/con_gl.php'); ?>

<?php

mysql_select_db($database_con_gl, $con_gl);

$query_cekpk = "SELECT gl_admin.username FROM gl_admin WHERE gl_admin.username='$_POST[username]'";

$cekpk = mysql_query($query_cekpk, $con_gl) or die(mysql_error());

$row_cekpk = mysql_fetch_assoc($cekpk);

$totalRows_cekpk = mysql_num_rows($cekpk);


$query_email = "SELECT * from gl_kontak where id = '$_POST[karyawan]'";

$cekemail = mysql_query($query_email, $con_gl) or die(mysql_error());
echo $cekemail;
$row_cekemail = mysql_fetch_assoc($cekemail);


$act = $_GET[act];

$url = "index.php?component=user";

$akses = $_POST[user_1].$_POST[user_2].$_POST[user_3].$_POST[user_4].$_POST[user_5].$_POST[jenisdok_1].$_POST[jenisdok_2].$_POST[jenisdok_3].$_POST[jenisdok_4].$_POST[statusdok_1].$_POST[statusdok_2].$_POST[statusdok_3].$_POST[statusdok_4].$_POST[dokumen_1].$_POST[dokumen_2].$_POST[dokumen_3].$_POST[dokumen_4].$_POST[dokumen_5].$_POST[laporan_1].$_POST[laporan_2].$_POST[laporan_3].$_POST[laporan_4].$_POST[kontak_1].$_POST[kontak_2].$_POST[kontak_3].$_POST[kontak_4].$_POST[unitkerja_1].$_POST[unitkerja_2].$_POST[unitkerja_3].$_POST[unitkerja_4].$_POST[unitbisnis_1].$_POST[unitbisnis_2].$_POST[unitbisnis_3].$_POST[unitbisnis_4].$_POST[karyawan_1].$_POST[karyawan_2].$_POST[karyawan_3].$_POST[karyawan_4].$_POST[infoper_1].$_POST[infoper_2].$_POST[infoper_3].$_POST[infoper_4];



/* Validasi */

if (trim($_POST['username']) == '') {

	$error[] = '- Username harus diisi !!!';

}

if($act=='add' AND $totalRows_cekpk > 0) {

	$error[] = '- Username sudah digunakan !!!';

}

if (trim($_POST['password']) == '') {

	$error[] = '- Password harus diisi !!!';

}

if (trim($akses) == '') {

	$error[] = '- Akses harus dicheck min 1 !!!';

}

/*End validasi */

function sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail,$ToName) {
        require("../lib/PHPMailer/class.phpmailer.php");
        $mail = new PHPMailer();   
        $mail->From     = $FromEmail;
        $mail->FromName = $FromName;         
        $mail->IsSMTP();        
        $mail->SMTPAuth = true;     // turn of SMTP authentication
        $mail->Username = "ricky_rinz@yahoo.co.id";  // SMTP username  (Ex: sumithnets@yahoo.com)
        $mail->Password = "rikiganteng"; // SMTP password  (Ex: yahoo email password)
        $mail->SMTPSecure = "ssl";        
        $mail->Host = "smtp.mail.yahoo.com";
        $mail->Port = 465;         
        $mail->SMTPDebug  = 2; // Enables SMTP debug information (for testing, remove this line on production mode)
         // 1 = errors and messages
         // 2 = messages only         
        $mail->Sender   =  $FromEmail;// $bounce_email;
        $mail->ConfirmReadingTo  = $FromEmail;         
        $mail->AddReplyTo($FromEmail);
        $mail->IsHTML(true); //turn on to send html email
        $mail->Subject = $Subject;        
        $mail->Body     =  $Message;
        $mail->AltBody  =  "ALTERNATIVE MESSAGE FOR TEXT WEB BROWSER LIKE SQUIRRELMAIL";      
       
				
		 $mail->AddAddress($ToEmail,$ToName);
		
		if($mail->Send()){
		 	 $mail->ClearAddresses();  
		}
		else
		{
			 echo "Mailer error: " . $email->ErrorInfo;
		}
 
 }
 
 
if (isset($error)) {

	echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);

}else{
	$pass = simple_encrypt($_POST['password']);
	if($act =='add') {
	
	$query = "INSERT INTO gl_admin (id, username, password, link, akses) VALUES (NULL, '$_POST[username]', '".simple_encrypt($_POST['password'])."', '$_POST[karyawan]', '$akses')";
	
	
	}else if($act=='edit'){

	$query ="UPDATE gl_admin SET username='$_POST[username]', password='".simple_encrypt($_POST['password'])."', link='$_POST[karyawan]', akses='$akses' WHERE id='$_POST[id]'";

	}else{

	$query = "delete from gl_admin where id='$_POST[id]'";

	}

	mysql_select_db($database_con_gl, $con_gl);

	$runquery = mysql_query($query, $con_gl);

	if($runquery) {
	 
	 $actual_link = "http://$_SERVER[HTTP_HOST]/research/index.php?component=document";
	 $FromEmail =   'ricky_rinz@yahoo.co.id'; //sumithnets@yahoo.com
	 $Subject   =   'DOCUMENT CONTROL ISO CIPAGANTI';  
	 
	 $Message  = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            </head>
            <body>
				<p>
				<h2><strong>Document Control System (ISO)</strong></h2>
				<br/>Management Representative
				<br/>
				<br/>Dear  <b>' . $row_cekemail['nama'] .'</b>
				<br/>
				<br/>
				<br />Anda sudah di daftarkan sebagai pengguna <b>DCMS ISO</b> dengan <b>username =' . $_POST['username'] .'</b>, dan <b>password =' . $_POST['password'] .'</b>. Anda sudah bisa menggunakan <b>Document Control Management System</b> untuk mengunduh document yang telah didistribusikan oleh Management Representative
				<br />Untuk menggunakan system ini, gunakan web browser; Internet Explorer, Mozila, Google Chrome
				<br />Silahkan klik link  <b>' . $actual_link . '</b> 
				<br />Lakukan login, dan pilih download document.
				<br />Tunggu Beberapa saat proses download.
				<br />
				<br />Untuk Info lebih lanjut, silahkan hubungi staff admin ISO : Fitri Nurpramanik
				</p>
				<p>
				Terima Kasih 
				</p>
				<p>				
				<br />
				<b>Cipaganti Citra Graha</b>
				<br /><b>Department<b> ISO</b>
				<br /><b>Support By:<b>IT DEV</b>
				</p>
            </body>
            </html>';
	 $FromName  =   'ISO Department';  // Sumith Harshan
	 $ToEmail   =  $row_cekemail['email'];
	 $ToName   =   $row_cekemail['nama'];
 	 
      //$response  = sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail,$ToName);
 	  
	
		echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Data berhasil ";
		if($act=='add' OR $act=='edit') {
			 sendEmail($FromEmail,$Subject,$Message,$FromName,$ToEmail,$ToName);
			 echo "disimpan";}else{ echo "dihapus"; } echo " ...";

	}else{

		echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Data gagal disimpan !!!";

	}

}
?>
<?php if($runquery) { ?><script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script><?php } ?>
?>

